<?php

require_once 'phpmoko/ui/Widget.php';

class DecoratedTabPanel extends Widget
{
	private $titles = array();
	private $components = array();
	private $ids = array();
	private $idTab = 0;
	private $selected = 0;
	private $animated = false;

	function __construct()
	{
		parent::__construct();

		require_once 'phpmoko/externals/JQueryUI.php';
		JQueryUI::activate();
	}

	function setAnimationEnabled($animated)
	{
		$this->animated = $animated;
	}

	function selectTab($selected)
	{
		$this->selected = $selected;
	}

	/**
	 * @param Widget $widget
	 * @param string $title
	 */
	function add($widget, $title)
	{
		$this->idTab++;
		$this->titles[] = $title;
		$this->components[] = $widget;
		$this->ids[] = $this->idTab;
	}

	function toString()
	{
		if ($this->getWidth() != null)
			$str .= '<div style="width: '.$this->getWidth().'">';

		$str .= '<div id="'.$this->id.'">';
		$str .= '<ul>';

		foreach ($this->titles as $key => $value)
			$str .= '<li><a href="#'.$this->id.'-'.$this->ids[$key].'"><span>'.$value.'</span></a></li>';

		$str .= '</ul>';

		foreach ($this->components as $key => $widget)
		{
			$str .= '<div id="'.$this->id.'-'.$this->ids[$key].'">';
			$str .= $widget->toString();
			$str .= '</div>';
		}

		$str .= '</div>';

		if ($this->getWidth() != null)
			$str .= '</div>';

		$str .= '<script type="text/javascript"> $(function() { $("#'.$this->id.'").tabs(';

		$options = '';

		if ($this->selected != 0)
			$options .= 'selected: '.$this->selected;

		if ($this->animated)
		{
			if ($options != '')
				$options .= ',';

			$options .= 'fx:{height: \'toggle\'}'; // opacity:\'toggle\'
		}

		if ($options != '')
			$str .= '{'.$options.'}';

		$str .= '); });</script>';

		return $str;
	}
}
?>